# Contributor: Timo Teras <timo.teras@iki.fi>
# Contributor: wener <wenermail@gmail.com>
# Maintainer: wener <wenermail@gmail.com>

# when changing _ver we *must* bump _rel
_name=dahdi-linux
_ver=3.1.0
_rel=0

_flavor=${FLAVOR:-lts}
_kpkg=linux-$_flavor
_kver=5.4.54
_krel=2

_kpkgver="$_kver-r$_krel"
_kabi="$_kver-$_krel-$_flavor"

pkgname=$_name-$_flavor
pkgver=$_kver
pkgrel=$(( $_krel + $_rel ))

pkgdesc="Digium Asterisk Hardware Device Interface drivers"
url="http://www.asterisk.org"
arch="x86_64"
license="GPL-2.0-or-later"
depends="dahdi-linux $_kpkg=$_kpkgver"
# we need wget and tar because make install downloads firmware and uses fancy
# options for tar and wget.
makedepends="$_kpkg-dev=$_kpkgver wget tar perl"
subpackages="$pkgname-dev"
source="http://downloads.digium.com/pub/telephony/dahdi-linux/releases/${_name}-$_ver.tar.gz
	dahdi-depmod.patch
	dahdi-bri_dchan.patch
	kernel-compat-5.0.patch
	kernel-compat-5.4.patch
	"

builddir="$srcdir/$_name-$_ver"

prepare() {
	default_prepare
	# verify the kernel version
	local _kapkbuild=../../main/linux-$_flavor/APKBUILD
	if [ -f $_kapkbuild ]; then
		(	. $_kapkbuild
			pkgname=$_name-$_flavor
			[ "$_kver" != "$pkgver" ] && die "please update _kver to $pkgver"
			[ "$_krel" != "$pkgrel" ] && die "please update _krel to $pkgrel"
			return 0
		)
	fi
}

build() {
	cd "$srcdir/$_name-$_ver"
	make KVERS="${_kabi}" DYNFS="yes"
}

package() {
	cd "$srcdir/$_name-$_ver"
	make KVERS="${_kabi}" DYNFS="yes" \
		DESTDIR="$pkgdir" install-modules
	rm -rf "$pkgdir"/lib/firmware "$pkgdir"/usr/lib/hotplug/firmware \
		"$pkgdir"/usr/include || true
}

# since we sourced the APKBUILD above we got the dev() function there to
# so we override it again.
depends_dev="dahdi-linux-dev"
dev() {
	default_dev
	local dir="$subpkgdir"/usr/src/dahdi-headers-${_kabi}
	install -D "$srcdir"/$_name-$_ver/drivers/dahdi/Module.symvers \
		"$dir"/drivers/dahdi/Module.symvers
	ln -s /usr/include "$dir"/include
}

sha512sums="440f5e44c933c609f2b3cfcf013c84bcd0c37929a43d69aac0276b11484cb890cd5939c5ac5950cad54d934d27ee6ff0b18f2bdd58477ed9be8055de107727b4  dahdi-linux-3.1.0.tar.gz
aba01eb90d9db1c595ff22078c76aadb20900486b5794d38b7ee61fa30d2fb51be318a4d059246017640447aee03fb09bb5514c818e3caa1cd98e331ec065173  dahdi-depmod.patch
fa2fa9e96eefc0f31a1a4a8f8f44e77936f6da0d7b74fe3c3d1f0de085621744ae4367932ac97a10b7f96b7d0954843b0bfe07a251b85434079cc6da29e243ba  dahdi-bri_dchan.patch
342f2467aae3ef00440d613a0bb4e8bd06cd1df2d4c9878ba8cf4af0eaa5b9cd7455c14e10c66c462f29812560622187bfb12b35b7b6cb466475a365dd998f40  kernel-compat-5.0.patch
a03fde27c801bd7a9f5267505072a50dc738dcac9cafb08539bbd38eb78f3bb688f067bbbcbe05521a6f38fe28d4e8bfee68c30acea24647a4ea0d28a05bd987  kernel-compat-5.4.patch"
